package com.hylanda.va.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.hylanda.va.dao.OrderRepository;
import com.hylanda.va.entity.SysOrders;
import com.hylanda.va.model.OrderAgg;
import com.hylanda.va.model.OrderAggOrderTotal;
import com.hylanda.va.model.OrderQuery;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年5月15日 下午4:22:59 
 * note
 */
@Service
public class OrderService {
	@Autowired
	public OrderRepository orderRepository;
	
	public SysOrders save(SysOrders entity){
		return orderRepository.save(entity);
	}

	public List<SysOrders> getList(SysOrders model) {
		return orderRepository.findAll();
	}
	
	public Page<SysOrders> findOrderNoCriteria(Integer page,Integer size){
		 Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "order_id");  
	     return orderRepository.findAll(pageable); 
	}
	public Page<SysOrders> findOrderCriteria(Integer page,Integer size,final OrderQuery orderQuery){
		 Pageable pageable = new PageRequest(page, size);  
	        Page<SysOrders> orderPage = orderRepository.findAll(new Specification<SysOrders>(){  
	            @Override  
	            public Predicate toPredicate(Root<SysOrders> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {  
	                List<Predicate> list = new ArrayList<Predicate>();  
	                if(null!=orderQuery.getOrder_name()&&!"".equals(orderQuery.getOrder_name())){  
	                    list.add(criteriaBuilder.equal(root.get("order_name").as(String.class), orderQuery.getOrder_name()));  
	                }  
	                if(0!=orderQuery.getOrder_id()){  
	                    list.add(criteriaBuilder.equal(root.get("order_id").as(Integer.class), orderQuery.getOrder_id()));  
	                }
	                if(0!=orderQuery.getClient_id()){  
	                    list.add(criteriaBuilder.equal(root.get("client_id").as(Integer.class), orderQuery.getClient_id()));  
	                } 
	                if(null!=orderQuery.getStudio()&&!"".equals(orderQuery.getStudio())){  
	                    list.add(criteriaBuilder.equal(root.get("studio").as(String.class), orderQuery.getStudio()));  
	                }
	                if(null!=orderQuery.getBeginTime()&&!"".equals(orderQuery.getBeginTime())
	                		&&null!=orderQuery.getEndTime()&&!"".equals(orderQuery.getEndTime())){
	                	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
	                	try {
							list.add(criteriaBuilder.between(root.<Date>get("create_time"), sdf2.parse(orderQuery.getBeginTime()), sdf2.parse(orderQuery.getEndTime())));
						} catch (ParseException e) {
							e.printStackTrace();
						}
	                }
	                if(null!=orderQuery.getStudioPersons()){
	                	Path<String> studio_persons = root.get("studio_persons");
	                	criteriaBuilder.like(studio_persons, "%" + orderQuery.getStudioPersons() + "%");
	                }
	                Predicate[] p = new Predicate[list.size()];
	                return criteriaBuilder.and(list.toArray(p));  
	            }  
	        },pageable);  
	        return orderPage;  
	}
//	CriteriaQuery q = cb.createQuery(Country.class);
//	Root c = q.from(Country.class);
//	q.select(c.get("currency"), cb.sum(c.get("population")));
//	q.where(cb.isMember("Europe", c.get("continents")));
//	q.groupBy(c.get("currency"));
//	g.having(cb.gt(cb.count(c), 1));

	public List<OrderAgg> getAggStudio(OrderQuery orderQuery) {
		Object[][] l=orderRepository.studioCostRank();
		List<OrderAgg> list=new ArrayList<OrderAgg>();
		for(int i=0;i<l.length;i++){
			OrderAgg agg =new OrderAgg();
			agg.setName((String)l[i][0]);
//			System.out.println(l[i][1].getClass());;
			agg.setValue((BigDecimal)l[i][1]);
			list.add(agg);
		}
		return list;
	}

	public List<OrderAggOrderTotal> getAggStudioOrderTotal(OrderQuery orderQuery) {
		Object[][] l=orderRepository.studioOrderTotalRank();
		List<OrderAggOrderTotal> list=new ArrayList<OrderAggOrderTotal>();
		for(int i=0;i<l.length;i++){
			OrderAggOrderTotal agg =new OrderAggOrderTotal();
			agg.setName((String)l[i][0]);
			agg.setValue((BigInteger)l[i][1]);
			list.add(agg);
		}
		return list;
	}
}
